package com.platform.server.util;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.functions.T;

import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.*;

/**
 * 用POI来生成相应的excel
 * Created by len.song on 2017/6/10.
 */
public class GenerateExcelPoiUtils {

    public static HSSFCellStyle createStyle(HSSFWorkbook workBook){
        HSSFCellStyle style = workBook.createCellStyle();// 创建样式对象
        //垂直水平居中
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        //单元格内自动换行
        style.setWrapText(true);
        return style;
    }

    /**
     * 生成excel的头部
     * @param heads 多个List<Map>组合而成  Map值组成  key：存的是对象的属性值   value：存的是生成excel表格的列标题
     * @param beginRow :起始行数
     * @param beginCol :起始列数
     * @return
     */
    public static Map<String,Object> generateHeader(HSSFWorkbook workBook,List<Map<String,String>> heads,Integer beginRow, Integer beginCol){
        Map<String,Object> map = new HashMap<String,Object>();
        if(workBook==null || (heads==null || heads.size()<1) || beginCol==null || beginCol.intValue()< 1 || beginRow==null || beginRow.intValue()<1 ){
            return null;
        }

        HSSFSheet sheet = workBook.createSheet();// 创建一个工作薄对象
        HSSFCellStyle style = createStyle(workBook);
        //设置标题
        HSSFRow titleRow = sheet.createRow(beginCol);
        titleRow.setRowStyle(style);
        List<String> cols = new ArrayList<>();
        for(Map<String,String> head: heads){
            for(Map.Entry<String,String> entry : head.entrySet()){
                String filedName = entry.getKey();
                String columnName = entry.getValue();
                titleRow.createCell(beginRow++).setCellValue(columnName);
                cols.add(filedName);
                break;
            }
        }
        map.put("headList",cols);
        map.put("sheet",sheet);
        return map;
    }

    /**
     * 生成excel的数据部分
     * @param sheet
     * @param datas
     * @param headLists
     * @param beginRow
     * @param beginCol
     * @param style
     * @return
     */
    public static HSSFSheet generateData(HSSFSheet sheet,List<?> datas,List<String> headLists,Integer beginRow,Integer beginCol,HSSFCellStyle style){
        if(sheet==null || (datas==null || datas.size()<1) || (headLists==null || headLists.size()<1) || beginCol==null
                || beginCol.intValue()< 1 || beginRow==null || beginRow.intValue()<1 ){
            return null;
        }
        try {
            for(Object t:datas){
                HSSFRow row = sheet.createRow(beginRow++);
                row.setRowStyle(style);
                Class c = t.getClass();
                Integer rowStartCol = beginCol;
                for(String col: headLists){
                    //获取需要执行属性的方法
                    //执行这些方法
                    Method method = c.getDeclaredMethod("get"+ col.substring(0,1).toUpperCase()+col.substring(1));
                    Type returnType = method.getGenericReturnType();// 返回类型
                    Object data = method.invoke(t);
                    if(data!=null){
                        if("java.util.Date".equals(returnType.getTypeName())){
                            row.createCell(rowStartCol++).setCellValue(DateUtil.format((Date)data,"yyyy-MM-dd HH:hh:ss"));
                        }else{
                            row.createCell(rowStartCol++).setCellValue(data+"");
                        }
                    }
                }
            }
        }catch (NoSuchMethodException e){
            e.printStackTrace();
        }catch (InvocationTargetException e){
            e.printStackTrace();
        }catch (IllegalAccessException e){
            e.printStackTrace();
        }

        return sheet;
    }

    //TODO
    public static void generateButton(){

    }

    public static HSSFWorkbook generateWorkBook(List<?> datas,List<Map<String,String>> heads,Integer beginRow,Integer beginCol){
        HSSFWorkbook workBook = new HSSFWorkbook();
        HSSFCellStyle style = createStyle(workBook);
        Map<String,Object> head = generateHeader(workBook,heads,beginRow,beginCol);
        if(head==null || head.get("headList")==null || head.get("sheet")==null){
            return null;
        }
        List<String> headColumsList = (List<String>)head.get("headList");
        HSSFSheet sheet = (HSSFSheet)head.get("sheet");
        generateData(sheet,datas,headColumsList,(beginRow+1),beginCol,style);
        return workBook;
    }
}
